package com.dormitory.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dormitory.manage.mapper.StudentMapper;
import com.dormitory.manage.pojo.Student;
import com.dormitory.manage.service.StudentService;
import com.dormitory.manage.vo.StudentQueryVo;
import com.dormitory.manage.vo.StudentUpdateVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @version 1.0
 * @description StudentServiceImpl
 * @date 2023/4/8 0008 9:49
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Override
    public List<String> getDormitoryMember(String dormitoryNo,Integer dormitoryBuildName) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_dormitory_build_name",dormitoryBuildName)
                .eq("student_dormitory_no",dormitoryNo);
        List<Student> students = this.baseMapper.selectList(queryWrapper);
        ArrayList<String> studentNames = new ArrayList<>();
        for (Student s: students) {
            studentNames.add(s.getStudentName());
        }
        return studentNames;
    }

    /**
    * @description 带条件分页查询学生列表
    * @param page
     * @param studentQueryVo
    * @returnType com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.dormitory.manage.pojo.Student>
    * @author Administrator
    * @date  20:47
    */
    @Override
    public Page<Student> getStudentPageList(Page<Student> page, StudentQueryVo studentQueryVo) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("student_college_name",studentQueryVo.getCollege())
                .like("student_profession",studentQueryVo.getProfession())
                .like("student_name",studentQueryVo.getStudentName());
        Page<Student> studentPage = this.baseMapper.selectPage(page, queryWrapper);
        return studentPage;
    }

    /**
    * @description 新增学生信息
    * @param student
    * @returnType boolean
    * @author Administrator
    * @date  21:23
    */
    @Override
    public boolean addStudent(Student student) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_name",student.getStudentName())
                .eq("student_gender",student.getStudentGender())
                .eq("student_dormitory_build_name",student.getStudentDormitoryBuildName())
                .eq("student_dormitory_no",student.getStudentDormitoryNo());
        Student student1 = this.baseMapper.selectOne(queryWrapper);
        if (student1 == null){
            student.setCheckInTime(new Date());
            return this.baseMapper.insert(student) > 0;
        }
        return false;
    }

    /**
    * @description 更新学生信息
    * @param studentId
     * @param studentUpdateVo
    * @returnType boolean
    * @author Administrator
    * @date  22:37
    */
    @Override
    public boolean updateStudent(long studentId, StudentUpdateVo studentUpdateVo) {
        Student student = this.baseMapper.selectById(studentId);
        if (null != student){
            student.setStudentPhone(studentUpdateVo.getStudentPhone());
            student.setStudentCollegeName(studentUpdateVo.getStudentCollegeName());
            student.setStudentProfession(studentUpdateVo.getStudentProfession());
            student.setStudentGrade(studentUpdateVo.getStudentGrade());
            student.setStudentClass(studentUpdateVo.getStudentClass());
            student.setStudentDormitoryBuildName(studentUpdateVo.getStudentDormitoryBuildName());
            student.setStudentDormitoryNo(studentUpdateVo.getStudentDormitoryNo());
            return this.baseMapper.updateById(student) > 0;
        }
        return false;
    }

    /**
    * @description 删除学生信息
    * @param studentId
    * @returnType
    * @author Administrator
    * @date  23:03
    */
    @Override
    public boolean deleteStudent(long studentId) {
        Student student = this.baseMapper.selectById(studentId);
        if (null != student){
            return this.baseMapper.deleteById(studentId) > 0;
        }
        return false;
    }
}
